package com.dhnsoft.shortLink.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhnsoft.shortLink.admin.common.convention.result.Result;
import com.dhnsoft.shortLink.admin.remote.dto.req.*;
import com.dhnsoft.shortLink.admin.remote.dto.resp.ShortLinkCountQueryRespDTO;
import com.dhnsoft.shortLink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.dhnsoft.shortLink.admin.remote.dto.resp.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ShortLinkRemoteService
 * Package: com.dhnsoft.shortLink.admin.remote.dto
 * Description: 后管远程调用接口
 *
 * @Author dhn
 * @Create 2024/3/24 15:24
 * @Version 1.0
 */
public interface ShortLinkRemoteService {


    /**
     * 远程调用中台短链接分页接口
     * */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }


    /**
     * 远程调用中台短链接创建接口
     * */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });
    }


    /**
     * 远程调用中台短链接列表接口
     * */
    default Result<List<ShortLinkCountQueryRespDTO>> listShortLinkCount(List<String> requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultBodyStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(resultBodyStr,new TypeReference<>(){});
    }


    /**
     * 远程调用中台短链接获取原始链接标题接口
     * */
    default Result<String> getTitleByUrl(String url){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("url",url);
        String resultBodyStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title", requestMap);
        return JSON.parseObject(resultBodyStr,new TypeReference<>(){});
    }

    /**
     * 远程调用中台短链接将短链接放入回收站功能
     * */
    default Result<Void> saveRecycleBin(RecycleBinSaveDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr,new TypeReference<>(){});
    }

    /**
     * 远程调用中台回收站短链接分页接口
     * */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam.getGidList());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    /**
     * 远程调用中台回收站短链接恢复接口
     * */
    default void recoverRecycleBinShortLink(RecycleBinRecoverDTO requestParam){
        String resultPost = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
    }

    /**
     * 远程调用中台回收站短链接移除接口
     * */
    default void removeRecycleBinShortLink(RecycleBinRemoveDTO requestParam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
    }
}
